package com.changggou.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author: zhengke
 * @date: 2020/12/11
 * @return:
 * @description: 第二个URL过滤器，用于记录用户请求的URL路径
 */

@Component
@Slf4j
public class UrlFilter implements GlobalFilter,Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();

        String path = request.getURI().getPath();
        log.info("第二个过滤器，记录的用户URL：{}",path);
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 1;//值要<=2，原因是stripPrefix过滤器的优先值是2，会优先于全局过滤器执行
    }
}
